package cn.kgc.springcloud.controller;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @author 课工场
 * @date 2025/1/9
 * @description
 */
@RestController
@RequestMapping("restTemplate")
public class RestTemplateController {


    @Resource
    private DiscoveryClient discoveryClient;

    @Resource
    private LoadBalancerClient loadBalancerClient;

    @Resource
    private RestTemplate restTemplate;

    // http://localhost:8512/consul/m1
    @GetMapping("m1")
    public String  method1(){
        // spring框架中提供的一个可以发送http请求的对象
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject("http://"+getRandomHost()+"/consul/m1", String.class);
        return " invoke RestTemplateController method1 ok =>  Consul => "+result;
    }


    @GetMapping("m2")
    public String  method2(){
        // spring框架中提供的一个可以发送http请求的对象
        RestTemplate restTemplate = new RestTemplate();
        // 获取通信节点  借助ribbon组件  从注册中心获取通信节点
        List<ServiceInstance> consulservice = discoveryClient.getInstances("CONSULSERVICE");
        Random random = new Random();
        int index = random.nextInt(consulservice.size());
        ServiceInstance serviceInstance = consulservice.get(index);
        String uri = serviceInstance.getUri().toString();

        String result = restTemplate.getForObject(uri + "/consul/m1", String.class);

        return " invoke RestTemplateController method1 ok =>  Consul => "+result;
    }

    @GetMapping("m3")
    public String  method3(){
        // 默认使用轮循策略获取通信节点
        ServiceInstance consulservice = loadBalancerClient.choose("CONSULSERVICE");
        RestTemplate restTemplate = new RestTemplate();
        String uri = consulservice.getUri().toString();
        String result = restTemplate.getForObject(uri +"/consul/m1", String.class);

        return " invoke RestTemplateController method1 ok =>  Consul => "+result;
    }


    //  该方法中的通信方式和前面几种形式相比 得到了简化 但仍存在硬编码问题  不便于后续代码维护      openfeign实现通信
    @GetMapping("m4")
    public String  method4(){
        String result = restTemplate.getForObject("http://CONSULSERVICE/consul/m1", String.class);
        return " invoke RestTemplateController method1 ok =>  Consul => "+result;
    }

    // 获取目标节点   负载均衡策略  随机  健康状态无法保证   ribbon 负载均衡   restTemplate+ribbon
    public String getRandomHost(){
        List<String> list = Arrays.asList("localhost:8510", "localhost:8511", "localhost:8512");
        Random random = new Random();
        int index = random.nextInt(list.size());
        return  list.get(index);
    }


}
